package engine.gui;

import org.lwjgl.input.*;

import engine.game.Manager;
import engine.graphics.Color4f;
import engine.input.IInput;
import engine.objects.ContainerObject;
import engine.system.IInit;

public class GUIObject extends ContainerObject implements IInit, IInput{

	private static final long serialVersionUID = 1L;
	
	protected static final String NORMAL_TEXTURE = "normal", 
								  HOVER_TEXTURE = "hover" ,
								  CLICK_TEXTURE = "click" ; 
	
	protected Color4f normalColor = null ; 
	protected Color4f hoverColor = null ; 
	protected Color4f clickColor = null ; 
	
	private boolean hoveringEnabled = true ; 
	private boolean clickingEnabled = true ;

	private boolean isVisible = true; 
	
	private boolean mouseWasDown, clicked; 
	
	private ParentObject parent ; 

	public GUIObject(Manager m, int x, int y, int w, int h) {
		super(m, x, y, w, h, false, true);
	}

	public GUIObject(Manager m, ParentObject parent){
		this(m, (int)parent.getX(), (int)parent.getY(), 
				(int)parent.getWidth(), (int)parent.getHeight()); 
		parent.addNewChild(this.toString(), this);
		this.parent = parent ; 
	}
	
	public GUIObject(Manager m, ParentObject parent, int w, int h){
		this(m, parent); 
		this.changeSize(w, h);
	}
		
	public ParentObject getParent(){
		return this.parent; 
	}
	
	public void init(){
		this.initVertices();
		this.changeNormalColor(new Color4f(0.25f, 0.25f, 0.25f, 1.0f)); 
		this.changeHoverColor(new Color4f(1.0f, 0.4f, 0.0f, 1.0f)); 
		this.changeClickColor(new Color4f(1.0f, 0.5f, 0.0f, 1.0f));
		this.disableDisplayList(); 
	}
	
	@Deprecated
	public void changeColor(Color4f color){
		super.changeColor(color);
	}
	
	public void changeNormalColor(Color4f color){
		this.normalColor = color ; 
	}

	public void changeHoverColor(Color4f color){
		this.hoverColor = color ; 
	}
	
	public void changeClickColor(Color4f color){
		this.clickColor = color ; 
	}
	
	public Color4f getNormalColor(){
		return this.normalColor ; 
	}
	
	public Color4f getHoverColor(){
		return this.hoverColor ; 
	}
	
	public Color4f getClickColor(){
		return this.clickColor ; 
	}
	
	public boolean isHovered(){
		return this.hoveringEnabled() && this.MouseIn() && !Mouse.isButtonDown(0); 
	}
	
	public boolean isMouseDown() {
		return this.clickingEnabled() && this.MouseIn() && Mouse.isButtonDown(0); 
	}
	
	public boolean isClicked(){
		return clicked ; 
	}
	
	public boolean hasColor(Color4f target){
		return target == this.getColor(); 
	}
	
	public void enableHovering(){
		this.hoveringEnabled = true ; 
	}
	
	public void disableHovering(){
		this.hoveringEnabled = false ; 
	}

	public boolean hoveringEnabled(){
		return this.hoveringEnabled; 
	}
	
	public void enableClicking(){
		this.clickingEnabled = true ; 
	}
	
	public void disableClicking(){
		this.clickingEnabled = false ; 
	}

	public boolean clickingEnabled(){
		return this.clickingEnabled; 
	}
	
	public void hide(){
		this.isVisible  = false; 
	}
	
	public void show(){
		this.isVisible = true ; 
	}
	
	public boolean isVisible(){
		return this.isVisible;
	}
		
	public void padding(int paddingLeft, int paddingTop){
		int x = (int) this.getX(); 
		int y = (int) this.getY(); 
		this.changeLocation(x+paddingLeft, y+paddingTop);
	}
	
	public void update(){
		if(this.isVisible){
			if(isMouseDown()) mouseWasDown = true ; 
			clicked = mouseWasDown && isHovered();
			if(clicked) mouseWasDown = false ; 
			super.update(); 
			if (this.isHovered()){
				this.changeColor(this.hoverColor);
//				this.changeAnimation(HOVER_TEXTURE); 
			}
			else if (this.isMouseDown()){
				this.changeColor(this.clickColor);
//				this.changeAnimation(CLICK_TEXTURE); 
			}
			else {
				if (!this.hasColor(this.normalColor)
						&& !(this.getColor() == null)){
					this.changeColor(this.normalColor);
//					this.changeAnimation(NORMAL_TEXTURE); 
				}
			}
		}
	}

	public void render(){
		if(this.isVisible)
			super.render(); 
	}

	@Override
	public void getInput() {
		if (this.isVisible) {
		}
	}
	
}
